#include "stack1.h"
#include <stdio.h>
#include <stdlib.h>
status initstack(stack *s){
	s->base=(redtype*)malloc(sizeof(redtype)*stacksize);
	if(!s->base) exit(overflow);
	s->top=s->base;
	return ok;
}
status destroy(stack *s){
	if(s->base)
		free(s->base);
	s->base=NULL;
	s->top=NULL;
	return ok;
}
status is_empty(stack *s){
	return s->top-s->base==0;
}
int get_length(stack *s){
	return s->top-s->base;
}
status get_top(stack *s,redtype **e){
	if(s->top-s->base==0)
		return underflow;
	redtype *elem=(redtype*)(s->top-1);
	*e=elem;
	return ok;
}
status clear_stack(stack *s){
	if(s->base)
		s->top=s->base;
	return ok;
}
status push(stack *s,redtype e){
	if(s->top-s->base==stacksize)
		return overflow;
	*s->top++=e;
	return ok;
}
status pop(stack *s,redtype **e){
	if(s->top-s->base==0)
		return underflow;
	*e=--s->top;
	return ok;
}
int main(){
    stack *s;
	initstack(s);
	printf("is empty:%d\n",is_empty(s));
	redtype r1;
	r1.elem='a';
	redtype r2;
	r2.elem='b';
	redtype r3;
	r3.elem='c';
	redtype r4;
	r4.elem='d';
	redtype r5;
	r5.elem='e';
	push(s,r1);
	push(s,r2);
	push(s,r3);
	push(s,r4);
	push(s,r5);
	printf("stack length:%d\n",get_length(s));
	redtype *r6;
	get_top(s,&r6);
	printf("get top:%c\n",r6->elem);
	redtype *r7;
	pop(s,&r7);
	printf("pop:%c\n",r7->elem);
	pop(s,&r7);
	printf("pop:%c\n",r7->elem);
	pop(s,&r7);
	printf("pop:%c\n",r7->elem);
	pop(s,&r7);
	printf("pop:%c\n",r7->elem);
	pop(s,&r7);
	printf("pop:%c\n",r7->elem);
	pop(s,&r7);
	printf("pop:%c\n",r7->elem);
	pop(s,&r7);
	printf("pop:%c\n",r7->elem);
}
